let timer = null;
import { getSupplierRoles } from "@/utils/vmeitime-http/localStorage";
export const customMixin = {
  install(app) {
    // 全局混入
    app.mixin({
      methods: {
        // 检查权限的方法
        checkRoles(bind = "", type = "menu") {
          try {
            const app = getApp();
            const roles = app?.globalData?.rolesObj || getSupplierRoles();
            return roles[type] && roles[type].find((role) => role === bind)
              ? true
              : false;
          } catch (error) {
            console.error("权限检查错误:", error);
            return false;
          }
        },

        // 防抖函数封装
        debounce(fn, delay = 1000) {
          return (function (...args) {
            if (timer) {
              clearTimeout(timer);
            }
            const that = this;
            timer = setTimeout(() => {
              fn.apply(that, args);
              timer = null;
            }, delay);
          })();
        },
      },

      onPullDownRefresh() {
        const pages = getCurrentPages();
        const currentpage = pages[pages.length - 1];
        currentpage.onLoad();
        currentpage.onShow();
        setTimeout(() => {
          uni.stopPullDownRefresh();
          uni.showToast({ title: "刷新成功",icon: "success"})
        }, 1000);
      },
    });
  },
};
